/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pingpong;

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

/**
 *
 * @author Morten Bormeth
 */
public class PanelBall extends JPanel implements Runnable{
    private static final long serialVersionUID = 1L;
    private int BallX = 10, BallY = 110, Player1X = 10, Player1Y = 100, Player2X = 370, Player2Y = 100;
    Thread Alpha;
    int mRight = 5; // to the right
    int mLeft = -5; //to the left
    int mUp = 5; // upward
    int mDown = -5; // down
    int bWidth, bHeight; // Width and height of the ball
    int scorePlayer1 = 0, scorePlayer2 = 0;  // Scores On start
    boolean player1KeyUp, player1KeyDown, player2KeyUp, player2KeyDown;
    boolean Playing, gameOver;
    int playingTo = 2;
    
    public PanelBall() {
        Playing = true;
        Alpha = new Thread(this);
        Alpha.start();
    }

    // Draw ball and Players
    @Override
   
    public void paintComponent(Graphics gc) {
        setOpaque(false);
        super.paintComponent(gc);

        // Draw ball
        gc.setColor(Color.white);
        gc.fillOval(BallX, BallY, 8, 8);
        

        // Draw Players
        gc.setColor(Color.GREEN);
        gc.fillRect(Player1X, Player1Y, 10, 25);
        gc.fillRect(Player2X, Player2Y, 10, 25);

        //Draw scores
        String Player1Name = "Morten";
        String Player2Name = "Martin";
        String PlayerWon;
        gc.drawString(Player1Name + ": " + scorePlayer1, Player1X, 10);
        gc.drawString(scorePlayer2 + " :" + Player2Name, Player2X - 30, 10);

        if (gameOver) {
            if(scorePlayer1 <= playingTo)
                PlayerWon = Player1Name;
            else
                PlayerWon = Player2Name;
            
            gc.drawString("Game Over." , 90, 125);
            gc.drawString(PlayerWon + " won." , 90, 140);
        }
    }

    public void drawBall(int nx, int ny) {
        BallX = nx;
        BallY = ny;
        this.bWidth = this.getWidth();
        this.bHeight = this.getHeight();
        repaint();
    }

    // Start moving player
    public void keyPressed(KeyEvent evt) {
        switch (evt.getKeyCode()) {
            // Move Player1
            case KeyEvent.VK_W:
                player1KeyUp = true;
                break;
            case KeyEvent.VK_S:
                player1KeyDown = true;
                break;

            // Move Player2
            case KeyEvent.VK_UP:
                player2KeyUp = true;
                break;
            case KeyEvent.VK_DOWN:
                player2KeyDown = true;
                break;
        }
    }

    //	Stop moving player
    public void keyReleased(KeyEvent evt) {
        switch (evt.getKeyCode()) {
            // Move Player1
            case KeyEvent.VK_W:
                player1KeyUp = false;
                break;
            case KeyEvent.VK_S:
                player1KeyDown = false;
                break;

            // Move Player2
            case KeyEvent.VK_UP:
                player2KeyUp = false;
                break;
            case KeyEvent.VK_DOWN:
                player2KeyDown = false;
                break;
        }
    }

    // Move Player1
    public void movePlayer1() {
        if (player1KeyUp == true && Player1Y >= 0) {
            Player1Y += mDown;
        }
        if (player1KeyDown == true && Player1Y <= (this.getHeight() - 25)) {
            Player1Y += mUp;
        }
        drawPlayer1(Player1X, Player1Y);
    }

    // Move Player2
    public void movePlayer2() {
        if (player2KeyUp == true && Player2Y >= 0) {
            Player2Y += mDown;
        }
        if (player2KeyDown == true && Player2Y <= (this.getHeight() - 25)) {
            Player2Y += mUp;
        }
        drawPlayer2(Player2X, Player2Y);
    }

    // Position on Y for the Player1
    public void drawPlayer1(int x, int y) {
        this.Player1X = x;
        this.Player1Y = y;
        repaint();
    }
    
    // Position on Y for the Player2   
    public void drawPlayer2(int x, int y) {
        this.Player2X = x;
        this.Player2Y = y;
        repaint();
    }

    public void playPling()
    {
     new Wave("pling.wav").start();
    }
    @Override
    public void run() {
        boolean vDirection = false;
        boolean hDirection = false;

        while (true) {

            if (Playing) {

                // The ball move from left to right
                if (vDirection) {
                    BallX += mRight;
                    if (BallX >= (bWidth - 8)) {
                        vDirection = false;
                         playPling();
                    }
                } else {
                    BallX += mLeft;
                    if (BallX <= 0) {
                        vDirection = true;
                         playPling();
                    }
                }


                // The ball moves from up to down
                if (hDirection) {
                    BallY += mUp;
                    if (BallY >= (bHeight - 8)) {
                        hDirection = false;
                         playPling();
                    }

                } else {
                    BallY += mDown;
                    if (BallY <= 0) {
                        hDirection = true;
                         playPling();
                    }
                }
                drawBall(BallX, BallY);

                // Delay
                try {
                    Thread.sleep(50);
                } catch (InterruptedException ex) {
                }

                // Move player 1
                movePlayer1();

                // Move player 2
                movePlayer2();

                // The score of the player 1 increase
                if (BallX >= (this.getWidth() - 8)) {
                    scorePlayer1++;
                }

                // The score of the player 2 increase
                if (BallX == 0) {
                    scorePlayer2++;
                }

                // Game over. Here you can change 6 to any value
                // When the score reach to the value, the game will end
                if (scorePlayer1 == playingTo || scorePlayer2 == playingTo) {
                    Playing = false;
                    gameOver = true;
                    new Wave("cheer.wav").start();
                }

                // The ball stroke with the player 1
                if (BallX <= Player1X + 10 && BallY >= Player1Y && BallY <= (Player1Y + 25)) {
                    vDirection = true;
                     playPling();
                }

                // The ball stroke with the player 2
                if (BallX >= (Player2X - 5) && BallY >= Player2Y && BallY <= (Player2Y + 25)) {
                    vDirection = false;
                    playPling();
                }
            }
        }
    }
}